package com.lqz.tools;

import java.util.Vector;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class PotTestUrl implements Runnable {

	//定义ReentrantLock锁
	public static ReentrantLock lock=new ReentrantLock();
	public static Condition condition=lock.newCondition();
	
	private Vector<?> rowData;
	private JTable tableList;
	private int rowsNum=0;
	public boolean flag = false;
	
	public PotTestUrl() {
		tableList = PotMain.tableList;
		DefaultTableModel tableModel = (DefaultTableModel) tableList.getModel();
		rowData = tableModel.getDataVector();
		//rowsNum = rowData.size();
	}
	
	public void run() {
		try{
			if(lock.tryLock(1, TimeUnit.SECONDS)){
				String rowString = new String(rowData.get(rowsNum).toString());
				tableList.setValueAt("检测中", rowsNum, 3);
				String[] row = rowString.substring(1, rowString.length() - 1).split(",");

				int a = Potplay.isVail(row[2], null);
				System.out.print(rowsNum+"--------");
				if (a == 200) {
					// System.out.println(row[2]+"========成功");
					tableList.setValueAt("可用", rowsNum, 3);
				} else if (a == 404) {
					tableList.setValueAt("不可用", rowsNum, 3);
				} else {
					tableList.setValueAt("连接超时", rowsNum, 3);
				}
				rowsNum++;
			}
		}catch(InterruptedException e){
			e.printStackTrace();
		}finally {
			if(lock.isHeldByCurrentThread())
				lock.unlock();
		}
		
		
	}
	public void run2() {
		String str = " ";

		int i=0;
		while (flag) {
			synchronized (str) {
				if (rowsNum > 0) {
					String rowString = new String(rowData.get(i).toString());
					tableList.setValueAt("检测中", i, 3);
					String[] row = rowString.substring(1, rowString.length() - 1).split(",");

					int a = Potplay.isVail(row[2], null);
					if (a == 200) {
						// System.out.println(row[2]+"========成功");
						tableList.setValueAt("可用", i, 3);
					} else if (a == 404) {
						tableList.setValueAt("不可用", i, 3);
					} else {
						tableList.setValueAt("连接超时", i, 3);
					}
					rowsNum--;
					i++;
				}
			}
		}
		
		/*for (int i = 0, n = rowData.size(); i < n; i++) {

			String rowString = new String(rowData.get(i).toString());
			tableList.setValueAt("检测中", i, 3);
			String[] row = rowString.substring(1, rowString.length() - 1).split(",");

			int a = Potplay.isVail(row[2], null);
			if (a == 200) {
				// System.out.println(row[2]+"========成功");
				tableList.setValueAt("可用", i, 3);
			} else if (a == 404) {
				tableList.setValueAt("不可用", i, 3);
			} else {
				tableList.setValueAt("连接超时", i, 3);
			}
			}
			*/

	}

}
